var BudgetList = {};
BudgetList.contextpath = "";
BudgetList.storyId="";
BudgetList.initPage = function(contextpath) {
	BudgetList.contextpath = contextpath;

	
	layui.use(['table','form'], function() {
		var table = layui.table;
		var form = layui.form;
		// 第一个实例
		table.render({
			elem : '#budget',
			toolbar : '#toolbarDemo'
			,
			url : '/web2019/user/ajaxQueryStoryBudgetList.action' // 数据接口
			,
			page : true // 开启分页
			,
			response : {
				statusName : 'code' // 规定数据状态的字段名称，默认：code
				,
				statusCode : "0000" // 规定成功的状态码，默认：0
				,
				msgName : 'message' // 规定状态信息的字段名称，默认：msg
				,
				countName : 'total' // 规定数据总数的字段名称，默认：count
			},
			cols : [ [ // 表头
			{
				field : 'storyId',
				title : 'storyId',
				hide  : true,
				width : 80,
				sort : true
			},{
				field : 'budgetId',
				title : '预算id',
				width : 150,
				sort : true
			}, {
				field : 'goods',
				title : '商品',
				width : 150,
				edit : 'text',
				sort : true
			},, {
				field : 'unitPrice',
				title : '单价',
				width : 150,
				edit : 'text',
				sort : true
			},{
				field : 'number',
				title : '数量',
				edit : 'text',
				width : 150
			},{
				field : 'money',
				title : '金额',
				edit : 'text',
				width : 150
			},{
				field : 'remark',
				title : '备注',
				edit : 'text',
				width : 200
			}, {
				title : '操作',
				templet : BudgetList.setOperate,
				width : 160,
				align : 'center',
				fixed : 'right'
			} ] ]
		});
		
		form.on('select(selectChange)', function(data){ 
			var storyId= $("#storyId").val();
			BudgetList.storyId=storyId;
			BudgetList.reloadTable(table,storyId);
			$("#storyId").val(storyId);
			layui.form.render('select');
		});
		
		// 监听单元格编辑
		table.on('tool(budget)', function(obj) {
			if(obj.event == 'delete'){
		      layer.confirm('真的删除行么', function(index){
		        BudgetList.deleteBudget(obj.data);
		        layer.close(index);
		      });
		    } else if(obj.event == 'add'){
		    	BudgetList.doAdd(obj.data)
		    }else if(obj.event == 'update'){
		    	BudgetList.doAdd(obj.data)
		    }
		});

	});
}
BudgetList.setOperate=function(data){
	var btnHtml = "";

	if (data.storyId != null) {
		btnHtml += '<button type="button" class="layui-btn layui-btn-xs" lay-event="update">更新</button>';
		btnHtml += '<button type="button" class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</button>';
	} else {
		btnHtml += '<button type="button" class="layui-btn layui-btn-xs" lay-event="add" >添加</button>';
	}
	return btnHtml;
}
BudgetList.reloadTable= function(table,storyId){
	table.reload('budget', {
        page: {
            curr: 1
        },
		where: 
		{
		storyId :storyId
	    }
    });
}
BudgetList.doAdd =function(data){
	data.storyId =BudgetList.storyId;
	if(BudgetList.storyId==""){
		layer.message("请选择项目");
		return;
	}
	Common.callClient("/web2019/user/ajaxSaveStoryBudget.action",data,function(data){
		if (data.code == "0000") {
			layer.alert(data.message, { icon: 1, closeBtn: 0 }, function (index) { 
				layer.close(index);
				var storyId= $("#storyId").val();
				layui.use(['table'], function() {
				var table = layui.table;
				table.reload('budget', {
					where : {
						"storyId" : storyId
					},
			    });
				});
				$("#storyId").val(storyId);
				
			});
		} else {
			layer.alert(data.message);
		}
	})
}

BudgetList.deleteBudget =function(data){
	Common.callClient("/web2019/user/ajaxDeleteStoryBudget.action",data,function(data){
		if (data.code == "0000") {
			layer.alert(data.message, { icon: 1, closeBtn: 0 }, function (index) { 
				layer.close(index);
				var storyId= $("#storyId").val();
				layui.use(['table'], function() {
				var table = layui.table;
				table.reload('budget', {
					where : {
						"storyId" : storyId
					},
			    });
				});
				$("#storyId").val(storyId);
				
			});
		} else {
			layer.alert(data.message);
		}
	})
	
}